package com.sld.cloudalibaba.controller;

import com.sld.cloudalibaba.pojo.Payment;
import com.sld.cloudalibaba.service.PaymentService;
import com.sld.cloudalibaba.vojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RefreshScope //在控制器类加入@RefreshScope注解使当前类下的配置支持Nacos的动态刷新功能。
public class PaymentController {

    @Autowired
    private PaymentService paymentService;
    @Value("${server.port}")
    private String port;
    @Value("${config.info}")
    private String info;
    @PostMapping("/payment")
    public Result save( @RequestBody Payment payment){
        int result = paymentService.create(payment);
        log.info("*************payment插入结果："+result);

        if (result > 0){
            log.info("*************payment插入的数据："+payment);
            return new Result(200,"插入数据库成功,port="+port,result);
        }else {
            return new Result(100,"插入失败,port="+port,null);
        }
    }

    @GetMapping("/payment/{id}")
    public Result getPaumentById(@PathVariable("id") Long id){
        Payment payment = paymentService.getPaymentById(id);
        log.info("**************payment查询结果："+payment);
        if (payment != null){
            return new Result(200,"查询成功,port="+port
                    +",config.info="+info,payment);
        }else {
            return new Result(100,"查询失败,port="+port
                    +",config.info="+info+"，id="+id,null);
        }
    }

}
